<?php
/*
 * @autor   Albert Moreno Vendrell
 */
Class Db{
    private $server;
    private $user;
    private $password;
    private $data_base;
    private $link;
    private $stmt;
    private $array;
    private $type;
    private $connection;

    static $_instance;

    /*
     * Construct the function is private to prevent the object can be created by new
     * @autor                           Albert Moreno Vendrell
     */
    private function __construct(){
        $this->setConection();
        $this->connect();
    }

    /*
     * Set conection parameters
     * @autor                           Albert Moreno Vendrell
     */
    private function setConection(){
        $configuration      = Configuration::getInstance();
        $this->server       = $configuration->getHostDB();
        $this->data_base    = $configuration->getDB();
        $this->user         = $configuration->getUserDB();
        $this->password     = $configuration->getPassDB();
        $this->type         = $configuration->getDBType();
    }

    /*
     * We avoid the cloning of the object. Singleton pattern
     */
        private function __clone(){ }

    /*
     * Function responsible for creating, if necessary, the object. 
     * This is the function must be called from outside the class to instantiate the object, and thus able to use its methods
     * @autor                           Albert Moreno Vendrell
     */
    public static function getInstance(){
        if (!(self::$_instance instanceof self)){
            self::$_instance=new self();
        }
        return self::$_instance;
    }

    /*
     * Make the connection to the database.
     * @autor                           Albert Moreno Vendrell
     */
    private function connect(){
        $this->connection   = new $this->type;

        $this->link         = $this->connection->connect();

        if (!$this->link){
            error_log(0,'Problem connecting to the database.');
            exit('Sorry for the inconvenience. We have a technical problem. We hope to resolve it in the next few minutes.');
        }else{
            $this->link=$link;
        }
    }

    /*
     * @autor                           Albert Moreno Vendrell
     */
    public function close(){
        //@TODO
    }

    /*
     * Method to execute a SQL statement
     * @autor                           Albert Moreno Vendrell
     */
    public function execute($sql){
        $this->stmt         = $this->connection->execute($sql);

        return $this->stmt;
    }

    /*
     * Method for obtaining a result row of sql statement
     * @autor                           Albert Moreno Vendrell
     */
    public function get_row($stmt,$row){
        $this->array    = $this->connection->get_row($stmt,$row);

        return $this->array;
    }

   /*
    * Returns the last insert id introduced
     * @autor                           Albert Moreno Vendrell
    */
   public function lastID(){
      return $this->connection->lastID();
   }

}